//assign1b.cpp

//***************************************************
//*												    *
//*	Student:		Jared Szechy	    		    *
//*	Login id:		cps350-N1.07			        *
//*	Assignment:		1b								*
//*	Date:			01/24/08						*
//*													*
//*	This program demonstrates the use of queues by	*
//*	creating a "Netflix" like movie queue.			*
//*													*
//***************************************************

#include <iostream>
#include <string>
#include <fstream>
#include "queue.h"
using namespace std;

//function to test copy constructor by passing queue by value
void TestCopy(EnhancedQueue<string> queue, ostream &outs);
//pre: none
//post: the member function Print is called and the queue is printed

int main()
{
	//declarations
	string inputFile;                    //the name of the input file
	string outputFile = "szechy1b.txt";  //the name of the output file  
	EnhancedQueue<string> queueArray[10];//the array of queues
	ifstream ins;                        //the input stream
	ofstream outs;                       //the output stream
	string command;                      //the operation to be performed 
	int queueNum1, queueNum2;            //used to specify which queues	
	int pos, from, to;					 //positions
	string item;                         //the item being put into, deleted 
										 //from, or searched for in the queue
	bool result;

	cout << "Please enter the name of the input file: ";
	cin >> inputFile;
	ins.open(inputFile.c_str());       //open input file
	outs.open(outputFile.c_str());     //open output file
	outs << "Jared Szechy" << endl;
	outs << "Input File: " << inputFile << endl << endl;

	//loop to process the input file line by line and call the correct
	//member function depending on the command word.
	while(ins >> command)
	{		
		ins >> queueNum1;
		if (command == "print")
		{
			outs << "Command line: " << command << " " << queueNum1 << endl;
			outs << "Queue " << queueNum1 << ":\n-------\n";
			queueArray[queueNum1].Print(outs);
		}		
		else if (command == "moveltof")
		{
			outs << "Command line: " << command << " " << queueNum1 << endl;
			queueArray[queueNum1].MoveLastToFront();
		}		
 		else if (command == "assign")
		{
			ins >> queueNum2;
			outs << "Command line: " << command << " " << queueNum1  << " " 
				<< queueNum2 << endl;
			queueArray[queueNum1] = queueArray[queueNum2];
		}		
 		else if (command == "enqueue")
		{
			ins >> item;
			outs << "Command line: " << command << " " << queueNum1 << " " 
				<< item << endl;
			queueArray[queueNum1].Enqueue(item);
		}		
		else if (command == "find")
		{
			ins >> item;
			outs << "Command line: " << command << " " << queueNum1 << " "
				<< item << endl;
			pos = queueArray[queueNum1].Find(item);
			if (pos == -1)
				outs << '\t' << item << " is not in queue " << queueNum1 << endl;
			else
				outs << '\t' << item << " is at position " << pos << " in queue " << queueNum1 << endl;
		}		
		else if (command == "length")
		{
			outs << "Command line: " << command << " " << queueNum1 << endl;
			outs << "\tThe length of queue " << queueNum1 << " is ";
			outs << queueArray[queueNum1].Length() << endl;
		}		
		else if (command == "clear")
		{
			outs << "Command line: " << command << " " << queueNum1 << endl;
			queueArray[queueNum1].Clear();
			outs << "\tQueue " << queueNum1 << " has been cleared." << endl;
		}		
		else if (command == "dequeue")
		{	
			outs << "Command line: " << command << " " << queueNum1 << endl;
			queueArray[queueNum1].Dequeue(item);
			outs << '\t' << item << " removed from queue " << queueNum1 << endl;
		}				
		else if (command == "delete")
		{	
			ins >> pos;
			outs << "Command line: " << command << " " << queueNum1 << " " << pos << endl;
			if (queueArray[queueNum1].Delete(pos,item))
				outs << '\t' << item << " removed from queue " << queueNum1 << endl;
			else
				outs << "\tDelete not sucessful; bad position." << endl;
		}				
		else if (command == "move")
		{	
			ins >> from;
			ins >> to;
			outs << "Command line: " << command << " " << queueNum1 << " " << from << " " << to << endl;
			if (queueArray[queueNum1].Move(from,to))
				outs << "\tMove was successful." << endl;
			else
				outs << "\tIllegal 'from' or 'to' values." << endl;
		}				
		else if (command == "testcopy")
		{
			outs << "Command line: " << command << " " << queueNum1 << endl;
			TestCopy(queueArray[queueNum1],outs);
		}		
	}  

	ins.close();
	outs.close();
	cout << "Program Complete...\n";
	return 0;
}

void TestCopy(EnhancedQueue<string> queue, ostream &outs)
{
	outs << "  Copy\n------\n";
	queue.Print(outs);
}